﻿using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Threading;
using System.Globalization;


using System.Net.Mail;

namespace KLS.PPDS.Utility
{
    public class CommonTask
    {

        public static void SendMail(string from, string to, string subject, string body)
        {
            var mailMessage = new MailMessage();
            var tos = to.Split(',');
            var froms = from.Split(',');
            
            
            foreach (var t in tos)
            {
                mailMessage.To.Add(t);
            }
            var f = new MailAddress(from, "E2A Admin");
            mailMessage.Subject = subject;
            mailMessage.IsBodyHtml = Utility.WebConfig.EmailFormatIsHtml;
            mailMessage.From = f;
            mailMessage.Body = body;
            mailMessage.BodyEncoding = System.Text.Encoding.UTF8;  
            SendMail(mailMessage);
        }

        public static void SendMail(MailMessage myMailMsg)
        {
            var oMailClient = new SmtpClient { Timeout = 120000 };
            oMailClient.Send(myMailMsg);
        }

        /// <summary>
        /// check the given color hex code is valid or not
        /// if not exists this return null.
        /// </summary>
        /// <param name="ColorCode"></param>
        /// <returns></returns>
        public static string CheckValidColorCode(string ColorCode)
        {
            return CheckValidColorCode(ColorCode, "");
        }

        public static void SetCulture(string selectedLanguage)
        {
            try
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage); // commented for beta release only
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);

                //Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(WebConfig.DefaultLanguage); // set defalut language fo beta releas
                //Thread.CurrentThread.CurrentUICulture = new CultureInfo(WebConfig.DefaultLanguage);
            }
            catch
            {
                
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(WebConfig.DefaultLanguage);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(WebConfig.DefaultLanguage);
            }
        }

        public static double GetFraction(double d)
        {

            var doubleString = d.ToString(CultureInfo.InvariantCulture);
            var lastIndex = doubleString.LastIndexOf(".");
            var result = 0.0;
            if (lastIndex > 0)
            {
                var s = doubleString.Substring(lastIndex, doubleString.Length - lastIndex);
                result = Convert.ToDouble(s,CultureInfo.InvariantCulture);
            }
            //throw new Exception("ss");
            return result;
        } 

        public static string CheckValidColorCode(string ColorCode, string defaultColor)
        {
            try
            {
                Color color = ColorTranslator.FromHtml("#" + ColorCode);
            }
            catch
            {
                if (string.IsNullOrWhiteSpace(defaultColor))
                    return null;
                else
                    return defaultColor;
            }
            return ColorCode;
        }
        public static bool IsNumber(string strTextEntry)
        {
            if (strTextEntry == null) return false;
            var objNotWholePattern = new Regex("[^0-9]");
            return !objNotWholePattern.IsMatch(strTextEntry)
                       && (strTextEntry != "");

        }
        public static bool IsMoney(string strTextEntry)
        {
            if (strTextEntry == null) return false;
            var objNotWholePattern = new Regex(@"\$\d+\.\d{2}");
            return !objNotWholePattern.IsMatch(strTextEntry)
                       && (strTextEntry != "");

        }
        public static bool IsValidEmail(string inputEmail)
        {
            if (inputEmail == null) return false;
            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
          @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
          @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            var re = new Regex(strRegex);
            if (re.IsMatch(inputEmail))
                return (true);
            return (false);

        }


        public static string MailBodyStart
        {
            get
            {
                return WebConfig.EmailFormatIsHtml ? "<!DOCTYPE HTML><html><head><title>E2A Mail</title><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /></head><body><table><tr><td>" : "";
            }
        }

        public static string MailBodyEnd
        {
            get
            {
                return WebConfig.EmailFormatIsHtml ? "</td></tr></table></body></html>" : "";
            }
        }

        public static string MailBodyNewLine
        {
            get
            {
                return WebConfig.EmailFormatIsHtml ? "<br/>" : Environment.NewLine;
            }
        }

    }
}
